/**
 * Created by Administrator on 2016/6/30.
 */
//本页公共变量声明
var nic_thumb = sessionStorage.nic_thumb;
var nic_name = sessionStorage.nic_name;
var username = sessionStorage.username;



$(function(){
    //判断是否登录-头像跳转
  $("header .portrait").click(function(){
      if(u_id){
          location.href = "changeInfo.html";
      }else {
          location.href = "register.html?jump=3";
      }
  });
    //用户登录状态操作
    if(u_id){
        //渲染用户头像
        if(nic_thumb == ''){
            $('.portrait').attr('src',userIconDefault);
            $('.nic_thumb').attr('src',userIconDefault);
        }else{
            $('.portrait').attr('src',nic_thumb);
            $('.nic_thumb').attr('src',nic_thumb);
        };
        //显示退出登录按钮
        $(".mineMenu section:nth-child(6)").css("display","inherit");
        //定义登录状态跳转
        $(" .mineMenu section:nth-child(1)").click(function(e){
            //提现跳转
            jumpPage("Earnings.html");
            e.stopPropagation();
        });
        $(" .mineMenu section:nth-child(2)").click(function(e){
            //关注列表跳转
            jumpPage("myattention.html");
            e.stopPropagation();
        });
        $(" .mineMenu section:nth-child(4)").click(function(e){
            //关注列表跳转
            jumpPage("mineNews.html");
            e.stopPropagation();
        });
        $(" .mineMenu section:nth-child(5)").click(function(e){
            jumpPage("feedback.html");
            e.stopPropagation();
        });
        //本页标题内容
        $("header p").text(nic_name);
    }else{
        //未登录状态跳转回当前页面
        $(" .mineMenu section:nth-child(1)").click(function(){
            jumpPage("register.html?jump=5");
            e.stopPropagation();
        });
        $(" .mineMenu section:nth-child(2)").click(function(e){
            jumpPage("register.html?jump=5");
            e.stopPropagation();
        });
        $(" .mineMenu section:nth-child(4)").click(function(e){
            jumpPage("register.html?jump=5");
            e.stopPropagation();
        });
        //本页标题内容
        $("header p").text("发肤");
    }
    //修改用户信息页面初始化变量
    $(" .mineMenuChange section:nth-child(2) span:nth-child(3)").text(nic_name);
    $(" .mineMenuChange section:nth-child(3) span:nth-child(3)").text(username);
    //调用修改头像
    $(".mineMenuChange section:first-child").click(function(){
        $("#uploadifive-file_upload input:last-child").click();
    })
   //修改昵称页面跳转
    $(" .mineMenuChange section:nth-child(2)").click(function(){
        jumpPage('changeNicName.html');
    });
    //修改手机号页面跳转
    $(" .mineMenuChange section:nth-child(3)").click(function(){
        jumpPage('changePhoneNumber.html');
    });
    //修改密码跳转
    $(" .mineMenuChange section:nth-child(4)").click(function(){
        jumpPage('changePassword.html');
    });
    //调用遮罩定义，弹窗位置大小
    $(" .mineMenu section:nth-child(6)").click(function(){
        var $height = $(window).height();
        $(".mask").css({'display':'inherit'});
        $('.go_or_not').css({'display':'inherit','position':'absolute','top':($height-220)/2,'left':'15%','z-index':'200'});
    });
    $('.go_or_not p:nth-child(2)').click(function(){
        $('.go_or_not').css('display','none');
        $('.mask').css('display','none');
    })

    $('.go_or_not p:nth-child(3)').click(function(){
        logOut();
    })
});






$(document).ready(function(){
    //请求用户信息
    postArray.u_id = u_id;
    $.post(commonval+"/user_info",postArray,function(data) {
        if(data.data.isDoctor==0){
            $("header img:last-child").css("display","none");
        }else{
            var d_id = data.data.d_id;
            sessionStorage.is_consult = data.data.is_consult;
            //显示医生主页入口
            $(".mineMenu section:nth-child(3)").css("display","block");
            $(".mineMenu section:nth-child(3)").click(function(){
                jumpPage("doctorInfo.html?id="+d_id);
            })
            //默认咨询状态显示图标
            if (sessionStorage.is_consult == 1) {
                $("header img:last-child").css("display","inherit");
                $("header img:last-child").attr("src","../images/closeConsult.png")
            } else if (sessionStorage.is_consult == 0) {
                $("header img:last-child").css("display","inherit");
                $("header img:last-child").attr("src","../images/openConsult.png")
            }
            //实时修改咨询和状态
            $("header img:last-child").click(function(){
                if (sessionStorage.is_consult == 1) {
                    var $height = $(window).height();
                    $(".go_or_not2").css("display","block");
                    $(".mask").css({'display':'inherit'});
                    $('.go_or_not2').css({'display':'inherit','position':'absolute','top':($height-220)/2,'left':'15%','z-index':'200'});
                    $('.go_or_not2 p:nth-child(3)').click(function(){
                        $('.go_or_not2').css('display','none');
                        $('.mask').css('display','none');
                        openCloseConsul(d_id);
                    })
                    $('.go_or_not2 p:nth-child(2)').click(function(){
                        $('.go_or_not2').css('display','none');
                        $('.mask').css('display','none');
                    });
                } else if (sessionStorage.is_consult == 0) {
                    openCloseConsul(d_id);
                }
            });
        }
    })
});

//定义本页公共函数

//退出登录
function logOut(){
    postArray.u_id = u_id;
    $.post(commonval+"/logout",postArray,function(data) {
        sessionStorage.clear();
        location.reload(true);
        $(".mineMenu section:nth-child(4)").css("display","none");
        createWindow("退出登录成功");
    })
}
//修改电话咨询状态
function openCloseConsul(d_id){
    postArray.d_id = d_id;
    $.post(commonval+"/openCloseConsul",postArray,function(data){
        if(data.status == 1){
            sessionStorage.is_consult = data.data.open_close_status;
            if(data.data.open_close_status == 0){
                $("header img:last-child").css("display","inherit");
                $("header img:last-child").attr("src","../images/openConsult.png")
                createWindow("关闭电话咨询成功");
            }else{
                $("header img:last-child").css("display","inherit");
                $("header img:last-child").attr("src","../images/closeConsult.png")
                createWindow("开启电话咨询成功");
            }
        }else{
            createWindow("修改电话咨询状态失败");
        }
    });
}

//uploadfive参数传递回调
$(function() {
    postArray.category ="nic_thumb";
    postArray.u_id = u_id;
    $('#file_upload').uploadifive({
        'fileObjName': 'upload',
        'auto'             : true,
        'formData'         : postArray,
        'removeCompleted' : true,
        'multi' : true,
        'uploadLimit' : 1,
        'simUploadLimit':1,
        'fileType' : 'image/*',
        'queueID'          : 'queue',
        'uploadScript'     : commonval+'/uploadLiveImage',
        'onUploadComplete' : function(file, data) {
            var splik = JSON.parse(data);
            //显示更换头像
            $(".mineMenuChange section:first-child .nic_thumb").attr("src",commonval+splik.data);
            var src = $(".mineMenuChange section:first-child .nic_thumb").attr("src");
            //ajax提交头像更换数据
            var nic_thumb = src.substr(length);
            postArray.nic_thumb = nic_thumb;
            $.post("http://dev.haotoufa.com/upUserThumb",postArray,function(data){
                if(data.status == "1"){
                    createWindow("修改头像成功");
                    jumpPage("changeInfo.html");
                    sessionStorage.nic_thumb = commonval+nic_thumb;
                }else{
                    createWindow(data.msg);
                }
            })
        }
    });
});
